package com.beiye.drivertransport.thematic.learning.finishedfragment;


import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.frame.adapter.CommonAdapter;
import com.android.frame.adapter.ViewHolder;
import com.beiye.drivertransport.R;
import com.beiye.drivertransport.SubActivity.LearnthemticlearningDetailsActivity;
import com.beiye.drivertransport.SubActivity.LearnthemticlearningDetailsAgainActivity;
import com.beiye.drivertransport.SubActivity.SignSpeciedlearningActivity;
import com.beiye.drivertransport.bean.CheckBean;
import com.beiye.drivertransport.bean.LearnthematiclearningBean;
import com.beiye.drivertransport.bean.LoginUserBean;
import com.beiye.drivertransport.bean.SpecifiedLearnAllBean;
import com.beiye.drivertransport.bean.SpecifiedPhotoBean;
import com.beiye.drivertransport.bean.ThematicLearnRecordBean;
import com.beiye.drivertransport.config.AppInterfaceConfig;
import com.beiye.drivertransport.fragment.TwoBaseFgt;
import com.beiye.drivertransport.http.Course;
import com.beiye.drivertransport.http.Login;
import com.beiye.drivertransport.thematic.learning.ThematicLearnRecordActivity;
import com.beiye.drivertransport.thematic.learning.examiantion.AdministratorcertificateActivity;
import com.beiye.drivertransport.utils.CameraCanUseUtils;
import com.beiye.drivertransport.utils.HelpUtil;
import com.beiye.drivertransport.utils.SharePerferenceUtils;
import com.beiye.drivertransport.utils.TiShiDialog;
import com.beiye.drivertransport.utils.ToastUtil;
import com.beiye.drivertransport.utils.UserManger;
import com.beiye.drivertransport.utils.Utils;
import com.beiye.drivertransport.view.MyListView;
import com.squareup.picasso.Picasso;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;



import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * A simple {@link Fragment} subclass.
 */
//学习
public class LearnthematiclearnedFragment extends TwoBaseFgt {

    MyListView lv_course;

    View empty_view;

    TextView tv_sign;//查看签名

    TextView tv_learn;//学时

    TextView tv_learn6;//完成情况
    ArrayList<LearnthematiclearningBean.RowsBean> courseList = new ArrayList<>();
    ArrayList<LearnthematiclearningBean.RowsBean> speciedList = new ArrayList<>();
    ArrayList<LearnthematiclearningBean.RowsBean> speciedList1 = new ArrayList<>();

    TextView fgLearnthematicTvHintMsg;

    TextView fgLearnThematicTvSeeCert;

    LinearLayout fgLearnthematicLlReapply;
    private LearningthematicAdapter learningthematicAdapter;
    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static String[] PERMISSIONS_STORAGE = {
            "android.permission.READ_EXTERNAL_STORAGE",
            "android.permission.WRITE_EXTERNAL_STORAGE"};

    private String forbidHour = "23,6";
    private int stId;
    private int position;
    private int uschSn;
    private static final int THEMATIC_LEARNRECORD_DATA=5;
    private static final int USERSTC_MOD_MARK = 4;
    private int sn;


    @Override
    public int getLayoutId() {
        return R.layout.fragment_learnthematiclearning;
    }


    @Override
    public void onClick(View view) {

    }

    @Override
    public void initData() {
        lv_course = (MyListView) getActivity().findViewById(R.id.lv_course);
        empty_view = (View) getActivity().findViewById(R.id.empty_view);
        tv_sign = (TextView)getActivity().findViewById(R.id.tv_sign);
        tv_learn= (TextView)getActivity().findViewById(R.id.tv_learn);
        tv_learn6 = (TextView)getActivity().findViewById(R.id.tv_learn6);
        fgLearnthematicTvHintMsg = (TextView) getActivity().findViewById(R.id.fg_learnthematic_tv_hintMsg);
        fgLearnThematicTvSeeCert = (TextView) getActivity().findViewById(R.id.fg_learnThematic_tv_seeCert);
        fgLearnthematicLlReapply = (LinearLayout) getActivity().findViewById(R.id.fg_learnthematic_ll_reapply);

        fgLearnthematicLlReapply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TiShiDialog.Builder builder = new TiShiDialog.Builder(getContext());
                builder.setMessage("重新申请需再次缴费，学习记录可在 '首页--专题学习记录' 查看");
                builder.setTitle("提示:");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        userStcModMark();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int i) {
                        dialog.dismiss();
                    }
                });
                builder.create().show();
            }
        });
        fgLearnThematicTvSeeCert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String certUrl="";
                if(stId == 630011 || stId == 630012){
                    certUrl = AppInterfaceConfig.BASE_URL + "app/spec/certSn?uschSn=" + uschSn;
                }else if(stId==320101){
                    certUrl = AppInterfaceConfig.BASE_URL + "app/spec/certJSSn?uschSn=" + uschSn;
                }
                Bundle bundle = new Bundle();
                bundle.putString("certUrl", certUrl);
                startActivity(AdministratorcertificateActivity.class, bundle);
            }
        });
        tv_sign.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (tv_sign.getText().toString().trim().equals("签名")) {
                    int uschSn = courseList.get(0).getUschSn();
                    Bundle bundle = new Bundle();
                    bundle.putInt("sn", uschSn);
                    bundle.putString("signPicUrl", "");
                    startActivity(SignSpeciedlearningActivity.class, bundle);
                } else if (tv_sign.getText().toString().trim().equals("查看签名")) {
                    int uschSn = courseList.get(0).getUschSn();
                    SharedPreferences sp = getContext().getSharedPreferences("LearnthematiclearningFragment", Context.MODE_PRIVATE);
                    String signPicUrl = sp.getString("signPicUrl", "");
                    Bundle bundle = new Bundle();
                    bundle.putInt("sn", uschSn);
                    bundle.putString("signPicUrl", signPicUrl);
                    startActivity(SignSpeciedlearningActivity.class, bundle);
                }
            }
        });


        lv_course.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //检测是否有写的权限
                int permission = ActivityCompat.checkSelfPermission(getActivity(),
                        "android.permission.WRITE_EXTERNAL_STORAGE");
                if (permission != PackageManager.PERMISSION_GRANTED) {
                    // 没有写的权限，去申请写的权限，会弹出对话框
                    ActivityCompat.requestPermissions(getActivity(), PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
                    return;
                }

                if(true){
                    return;
                }

                if (stId == 510016 || stId == 510017 || stId == 510018 || stId == 510019) { //四川继续教育专题培训
                    if (!forbidHour.equals("") && !forbidHourJudge()) {
                        HelpUtil.showTiShiDialog(getContext(), "安全第一，现在是休息时间，明天再来学吧！");
                        return;
                    }
                }


                if (!CameraCanUseUtils.isCameraCanUse()) {

                    ToastUtil.showShortToast(getActivity(), "请到手机设置界面里找驾运宝允许开启照相");

                } else {
                    if (learningthematicAdapter.getItem(position).getMark() == 0) {
                        int sn = learningthematicAdapter.getItem(position).getSn();
                        int uschSn = learningthematicAdapter.getItem(position).getUschSn();
                        int tcSn = learningthematicAdapter.getItem(position).getTcSn();
                        int readLength = learningthematicAdapter.getItem(position).getReadLength();
                        speciedList.clear();
                        for (int i = 0; i < courseList.size(); i++) {
                            if (courseList.get(i).getTcSn() != tcSn && courseList.get(i).getMark() != 1) {
                                speciedList.add(courseList.get(i));
                            }
                        }
                        SharedPreferences sp = getContext().getSharedPreferences("LearnthematiclearningFragment", Context.MODE_PRIVATE);
                        int minPer = sp.getInt("minPer", 0);
                        int tphotoMark = sp.getInt("tphotoMark", 0);
                        int stId = sp.getInt("stId", 0);
                        int seqTMark = sp.getInt("seqTMark", 0);
                        int apromptMark = sp.getInt("apromptMark", 0);
                        //专题培训课程必须顺序学习；
                        if (position == 0) {
                            showLoadingDialog("");
                            Bundle bundle = new Bundle();
                            bundle.putInt("tcsn", tcSn);
                            bundle.putInt("uschSn", uschSn);//专项培训用户学时序列号
                            bundle.putInt("sn", sn);//专项培训用户课程序列号
                            bundle.putInt("readLength", readLength);//保存时间
                            bundle.putSerializable("CourseDetailList", (Serializable) speciedList);
                            bundle.putInt("minPer", minPer);
                            bundle.putInt("tphotoMark", tphotoMark);
                            bundle.putInt("stId", stId);
                            bundle.putInt("apromptMark", apromptMark);
                            //startActivity(LearnthemticlearningDetailsActivity.class, bundle);
                            return;
                        }
                        speciedList1.clear();
                        for (int i = 0; i <= position - 1; i++) {
                            if (courseList.get(i).getMark() == 0) {
                                speciedList1.add(courseList.get(i));
                            }
                        }
                        if (seqTMark == 1 && speciedList1.size() > 0) {
                            TiShiDialog.Builder builder1 = new TiShiDialog.Builder(getContext());
                            builder1.setMessage("上一节课程需学习完成，才能继续当前课程");
                            builder1.setTitle("提示:");
                            builder1.setPositiveButton("我知道", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();

                                }
                            });
                            builder1.create().show();

                            return;
                        }
                        showLoadingDialog("");
                        Bundle bundle = new Bundle();
                        bundle.putInt("tcsn", tcSn);
                        bundle.putInt("uschSn", uschSn);//专项培训用户学时序列号
                        bundle.putInt("sn", sn);//专项培训用户课程序列号
                        bundle.putInt("readLength", readLength);//保存时间
                        bundle.putSerializable("CourseDetailList", (Serializable) speciedList);
                        bundle.putInt("minPer", minPer);
                        bundle.putInt("tphotoMark", tphotoMark);
                        bundle.putInt("stId", stId);
                        bundle.putInt("apromptMark", apromptMark);
                        //startActivity(LearnthemticlearningDetailsActivity.class, bundle);

                    } else if (learningthematicAdapter.getItem(position).getMark() == 1) {

                        TiShiDialog.Builder builder1 = new TiShiDialog.Builder(getContext());
                        builder1.setMessage("您已学习了该课程，继续浏览课程将不记录时间");
                        builder1.setTitle("提示:");
                        builder1.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                SharedPreferences sp = getContext().getSharedPreferences("LearnthematiclearningFragment", Context.MODE_PRIVATE);
                                int stId = sp.getInt("stId", 0);
                                int tcSn = learningthematicAdapter.getItem(position).getTcSn();
                                Bundle bundle = new Bundle();
                                bundle.putInt("tcsn", tcSn);
                                bundle.putInt("stId", stId);
                                //startActivity(LearnthemticlearningDetailsAgainActivity.class, bundle);
                                //设置你的操作事项
                            }
                        });
                        builder1.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();

                            }
                        });
                        builder1.create().show();

                    }
                }
            }
        });
    }

    @Override
    public void requestData() {
        String orgId = getArguments().getString("orgId");
        stId = getArguments().getInt("stId");
        sn=getArguments().getInt("sn");
        String rolename = getArguments().getString("rolename");
        String adId = getArguments().getString("adId");
        position = getArguments().getInt("position");
        int seqTMark = getArguments().getInt("seqTMark");
        SharedPreferences sp = getContext().getSharedPreferences("LearnthematiclearningFragment", Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();
        edit.putString("orgId", orgId);
        edit.putString("rolename", rolename);
        edit.putString("adId", adId);
        edit.putInt("stId", stId);
        edit.putInt("position", position);
        edit.putInt("seqTMark", seqTMark);
        edit.commit();
        LoginUserBean userInfo = UserManger.getUserInfo();
        LoginUserBean.DataBean data = userInfo.getData();
        String userId = data.getUserId();
        showLoadingDialog("");

        if (stId == 650017 || stId == 650016 || stId == 650015 || stId == 650014 || stId == 650013 || stId == 650012 || stId == 650011 || stId == 650010) {
            fgLearnthematicTvHintMsg.setVisibility(View.VISIBLE);
        } else {
            fgLearnthematicTvHintMsg.setVisibility(View.GONE);
        }

        getThematicLearnRecord();
        new Login().getRolespecifiedlearncourse(orgId, userId, stId, sn+"",this, 1);//专项学习课程


    }

    @Override
    public void onResume() {
        super.onResume();
        SharedPreferences sp = getContext().getSharedPreferences("LearnthematiclearningFragment", Context.MODE_PRIVATE);
        String adId = sp.getString("adId", "");
        String orgId = sp.getString("orgId", "");
        String rolename = sp.getString("rolename", "");
        int stId = sp.getInt("stId", 0);
        LoginUserBean userInfo = UserManger.getUserInfo();
        LoginUserBean.DataBean data = userInfo.getData();
        String userId = data.getUserId();


        getThematicLearnRecord();
        new Login().getRolespecifiedlearncourse(orgId, userId, stId, sn+"",this, 2);//刷新专项学习课程
    }

    @Override
    public void onSuccess(String result, Call call, Response response, int what) {
        super.onSuccess(result, call, response, what);
        if (what == 1) {
            dismissLoadingDialog();
            LearnthematiclearningBean learnthematiclearningBean = JSON.parseObject(result, LearnthematiclearningBean.class);
            List<LearnthematiclearningBean.RowsBean> rows = learnthematiclearningBean.getRows();
            courseList.clear();
            if (rows.size() == 0) {
                lv_course.setEmptyView(empty_view);
                return;
            }
            uschSn = rows.get(0).getUschSn();
            courseList.addAll(rows);
            learningthematicAdapter = new LearningthematicAdapter(getActivity(), courseList, R.layout.learnthematic_item_layout);
            lv_course.setAdapter(learningthematicAdapter);

        } else if (what == 2) {

            LearnthematiclearningBean learnthematiclearningBean = JSON.parseObject(result, LearnthematiclearningBean.class);
            List<LearnthematiclearningBean.RowsBean> rows = learnthematiclearningBean.getRows();
            courseList.clear();
            if (rows.size() == 0) {
                lv_course.setEmptyView(empty_view);
                return;
            }

            courseList.addAll(rows);
            learningthematicAdapter = new LearningthematicAdapter(getActivity(), courseList, R.layout.learnthematic_item_layout);
            lv_course.setAdapter(learningthematicAdapter);
        } else if (what == 3)//统计那块
        {
            /*dismissLoadingDialog();
            SpecifiedLearnAllBean specifiedLearnAllBean = JSON.parseObject(result, SpecifiedLearnAllBean.class);
            List<SpecifiedLearnAllBean.RowsBean> rows = specifiedLearnAllBean.getRows();
            for (int i = 0; i < rows.size(); i++) {
                if (rows.get(i).getShowTrml() != 3) {
                    jiayunbaoSpecified.add(rows.get(i));
                }
            }
            SharedPreferences sp = getContext().getSharedPreferences("LearnthematiclearningFragment", Context.MODE_PRIVATE);
            int position = sp.getInt("position", 0);
            Integer chNo = jiayunbaoSpecified.get(position).getChNo();
            Integer finishMark = jiayunbaoSpecified.get(position).getFinishMark();
            String signPicUrl = HelpUtil.judgeStrNull(jiayunbaoSpecified.get(position).getSignPicUrl());
            Integer minPer = jiayunbaoSpecified.get(position).getMinPer();
            Integer tphotoMark = jiayunbaoSpecified.get(position).getTphotoMark();
            Integer apromptMark = jiayunbaoSpecified.get(position).getApromptMark();
            int uploadMark=jiayunbaoSpecified.get(position).getUploadMark();

            Integer thematicFaceRecgMark = jiayunbaoSpecified.get(position).getFaceRecgMark();
            SharePerferenceUtils.putInt(getContext(), "thematicFaceRecgMark", thematicFaceRecgMark);

            if (stId == 630011 || stId == 630012) {
                if (finishMark > 0) {
                    //fgLearnThematicTvSeeCert.setVisibility(View.VISIBLE);
                }
            }
            if(stId==320101){
                if(uploadMark==1 && finishMark>0){
                    fgLearnThematicTvSeeCert.setVisibility(View.VISIBLE);
                }
            }

            SharedPreferences.Editor edit1 = sp.edit();
            edit1.putInt("minPer", minPer);
            edit1.putInt("tphotoMark", tphotoMark);
            edit1.putInt("apromptMark", apromptMark);
            edit1.commit();
            if (chNo == 0) {
                tv_learn.setText(0 + "学时");
            } else if (chNo > 0) {
                tv_learn.setText(chNo + "学时");
            }
            if (finishMark == 1) {
                //fgLearnthematicLlReapply.setVisibility(View.VISIBLE);
                tv_learn6.setText("已完成未签名");
                tv_learn6.setTextColor(getActivity().getResources().getColor(R.color.holetotal_yellow));
                tv_sign.setVisibility(View.VISIBLE);
            }
            if (finishMark == 2) {
                //fgLearnthematicLlReapply.setVisibility(View.VISIBLE);
                tv_learn6.setText("已完成已签名");
                tv_learn6.setTextColor(getActivity().getResources().getColor(R.color.project_gray));
                tv_sign.setVisibility(View.VISIBLE);

            } else if (finishMark == 0) {
                //fgLearnthematicLlReapply.setVisibility(View.GONE);
                tv_learn6.setText("未完成");
                tv_learn6.setTextColor(getActivity().getResources().getColor(R.color.project_redsecond));
                tv_sign.setVisibility(View.GONE);
            }

            if (signPicUrl.equals("")) {
                tv_sign.setText("签名");
            } else {
                SharedPreferences sp1 = getContext().getSharedPreferences("LearnthematiclearningFragment", Context.MODE_PRIVATE);
                SharedPreferences.Editor edit = sp1.edit();
                edit.putString("signPicUrl", signPicUrl);
                edit.commit();
                tv_sign.setText("查看签名");
            }*/
        }else if(what==USERSTC_MOD_MARK){
            showToast(getContext(),"重置成功");
            getActivity().finish();
        }else if(what==THEMATIC_LEARNRECORD_DATA){
            ThematicLearnRecordBean bean= JSON.parseObject(result,ThematicLearnRecordBean.class);
            List<ThematicLearnRecordBean.RowsBean> rowsBeans = bean.getRows();
            SharedPreferences sp = getContext().getSharedPreferences("LearnthematiclearningFragment", Context.MODE_PRIVATE);
            int position = sp.getInt("position", 0);
            int chNo = rowsBeans.get(position).getChNo();
            int finishMark = rowsBeans.get(position).getFinishMark();
            String signPicUrl = HelpUtil.judgeStrNull(rowsBeans.get(position).getSignPicUrl());
            /*Integer minPer = rowsBeans.get(position).getMinPer();
            Integer tphotoMark = rowsBeans.get(position).getTphotoMark();
            Integer apromptMark = rowsBeans.get(position).getApromptMark();*/
            int uploadMark=rowsBeans.get(position).getUploadMark();

            if (stId == 630011 || stId == 630012) {
                if (finishMark > 0) {
                    //fgLearnThematicTvSeeCert.setVisibility(View.VISIBLE);
                }
            }
            if(stId==320101){
                if(uploadMark==1 && finishMark>0){
                    fgLearnThematicTvSeeCert.setVisibility(View.VISIBLE);
                }
            }

            tv_learn.setText(chNo + "学时");
            if (finishMark == 1) {
                tv_learn6.setText("已完成未签名");
                tv_learn6.setTextColor(getActivity().getResources().getColor(R.color.holetotal_yellow));
                tv_sign.setVisibility(View.VISIBLE);
            }
            if (finishMark == 2) {
                tv_learn6.setText("已完成已签名");
                tv_learn6.setTextColor(getActivity().getResources().getColor(R.color.project_gray));
                tv_sign.setVisibility(View.VISIBLE);

            } else if (finishMark == 0) {
                tv_learn6.setText("未完成");
                tv_learn6.setTextColor(getActivity().getResources().getColor(R.color.project_redsecond));
                tv_sign.setVisibility(View.GONE);
            }

            if (signPicUrl.equals("")) {
                tv_sign.setText("签名");
            } else {
                SharedPreferences sp1 = getContext().getSharedPreferences("LearnthematiclearningFragment", Context.MODE_PRIVATE);
                SharedPreferences.Editor edit = sp1.edit();
                edit.putString("signPicUrl", signPicUrl);
                edit.commit();
                tv_sign.setText("查看签名");
            }
        }
    }

    @Override
    public void onError(String result, Call call, Response response, int what) {
        if (what == 1) {
            dismissLoadingDialog();
            CheckBean checkBean = JSON.parseObject(result, CheckBean.class);
            String msg = checkBean.getMsg();
            TiShiDialog.Builder builder1 = new TiShiDialog.Builder(getContext());
            builder1.setMessage(msg);
            builder1.setTitle("提示:");
            builder1.setPositiveButton("我知道", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    ;
                    //设置你的操作事项
                }
            });
            builder1.create().show();
        }
        super.onError(result, call, response, what);
    }

    @Override
    public void onUserVisible() {
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();

    }

    public class LearningthematicAdapter extends CommonAdapter<LearnthematiclearningBean.RowsBean> {
        private Context context;
        private List<LearnthematiclearningBean.RowsBean> mList;
        private PopupWindow mPopWindow;


        public LearningthematicAdapter(Context context, List<LearnthematiclearningBean.RowsBean> mList, int itemLayoutId) {
            super(context, mList, itemLayoutId);
            this.context = context;
            this.mList = mList;
        }


        @Override
        public void convert(ViewHolder holder, final LearnthematiclearningBean.RowsBean item, final int positon) {

            final TextView tv_title = holder.getView(R.id.tv_title);
            TextView tv_finish = holder.getView(R.id.tv_finish);
            TextView tv_learnphoto = holder.getView(R.id.tv_learnphoto);//学习照片
            TextView tv_learnline = holder.getView(R.id.tv_learnline);
            final TextView tv_learn = holder.getView(R.id.tv_learn);
            TextView tv_time = holder.getView(R.id.tv_time);


            String courseName = mList.get(positon).getCourseName();
            int courseLength = mList.get(positon).getCourseLength();
            if (courseName == null) {
                tv_title.setText("");
            } else {
                tv_title.setText(courseName);
            }

            if (courseLength == 0) {
                tv_time.setText("分钟");
            } else {
                tv_time.setText(courseLength + "分钟");
            }

            int mark = mList.get(positon).getMark();
            int readLength = mList.get(positon).getReadLength();

            if (mark == 0) {
                if (readLength == 0) {
                    tv_learn.setVisibility(View.VISIBLE);
                    tv_learn.setText("开始学习");
                    tv_finish.setText("未学习");
                    tv_finish.setBackgroundResource(R.drawable.shape_specified_nored);
                    tv_learnline.setBackgroundResource(R.drawable.learnthematic_red_line);
                    tv_learnphoto.setVisibility(View.GONE);
                } else if (readLength > 0) {
                    tv_learn.setVisibility(View.VISIBLE);
                    tv_learn.setText("继续学习");
                    tv_finish.setText("未学完");
                    tv_finish.setBackgroundResource(R.drawable.shape_specified_noorange);
                    tv_learnline.setBackgroundResource(R.drawable.learnthematic_orange_line);
                    tv_learnphoto.setVisibility(View.GONE);
                }
            } else if (mark == 1) {
                tv_learn.setVisibility(View.GONE);
                tv_finish.setText("已学习");
                tv_finish.setBackgroundResource(R.drawable.shape_specified_yesgray);
                tv_learnphoto.setVisibility(View.VISIBLE);
                tv_learnline.setBackgroundResource(R.drawable.learnthematic_gray_line);
            }
            tv_learnphoto.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (Utils.isFastClicker()) {
                        return;
                    }
                    RequestParams params = new RequestParams(AppInterfaceConfig.BASE_URL + "course/userStcPhoto/findByUsCourseSn/" + mList.get(positon).getSn());
                    x.http().get(params, new Callback.CommonCallback<String>() {
                        @Override
                        public void onSuccess(String result) {
                            dismissLoadingDialog();
                            SpecifiedPhotoBean specifiedPhotoBean = JSON.parseObject(result, SpecifiedPhotoBean.class);
                            List<SpecifiedPhotoBean.RowsBean> rows = specifiedPhotoBean.getRows();

                            String photourl="";
                            if(rows.size()>0){
                                photourl=rows.get(0).getUrl();
                            }
                            if (photourl.equals("")) {
                                TiShiDialog.Builder builder1 = new TiShiDialog.Builder(getContext());
                                builder1.setMessage("您在本课程没有学习照片。");
                                builder1.setTitle("提示:");
                                builder1.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                        ;
                                        //设置你的操作事项
                                    }
                                });
                                builder1.create().show();
                            } else {
                                View contentView = LayoutInflater.from(getContext()).inflate(R.layout.popwindow_showphoto_layout, null);
                                mPopWindow = new PopupWindow(contentView, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT, true);
                                mPopWindow.showAtLocation(tv_title, Gravity.CENTER, 0, 0);
                                TextView tv_dismiss = (TextView) contentView.findViewById(R.id.tv_dismiss);
                                tv_dismiss.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        mPopWindow.dismiss();
                                    }
                                });
                                final ImageView img_photo1 = (ImageView) contentView.findViewById(R.id.img_photo1);

                                if (photourl.contains("aliyuncs.com")) {
                                    Picasso.with(context).load(Uri.parse(photourl)).placeholder(R.mipmap.no_data).into(img_photo1);
                                } else {
                                    Picasso.with(context).load(Uri.parse(AppInterfaceConfig.getRequestUrl(photourl))).placeholder(R.mipmap.no_data).into(img_photo1);
                                }
                            }
                        }

                        @Override
                        public void onError(Throwable ex, boolean isOnCallback) {
                            // showToast("网络连接错误");
                        }

                        @Override
                        public void onCancelled(CancelledException cex) {

                        }

                        @Override
                        public void onFinished() {

                        }
                    });
                }
            });
            tv_learn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //检测是否有写的权限
                    int permission = ActivityCompat.checkSelfPermission(getActivity(),
                            "android.permission.WRITE_EXTERNAL_STORAGE");
                    if (permission != PackageManager.PERMISSION_GRANTED) {
                        // 没有写的权限，去申请写的权限，会弹出对话框
                        ActivityCompat.requestPermissions(getActivity(), PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
                        return;
                    }

                    if (stId == 510016 || stId == 510017 || stId == 510018 || stId == 510019) { //四川继续教育专题培训
                        if (!forbidHour.equals("") && !forbidHourJudge()) {
                            HelpUtil.showTiShiDialog(getContext(), "安全第一，现在是休息时间，明天再来学吧！");
                            return;
                        }
                    }

                    if (!CameraCanUseUtils.isCameraCanUse()) {

                        ToastUtil.showShortToast(getActivity(), "请到手机设置界面里找驾运宝允许开启照相");

                    } else {
                        if (mList.get(positon).getMark() == 0) {
                            int sn = mList.get(positon).getSn();
                            int uschSn = mList.get(positon).getUschSn();
                            int tcSn = mList.get(positon).getTcSn();
                            int readLength = mList.get(positon).getReadLength();
                            speciedList.clear();
                            for (int i = 0; i < courseList.size(); i++) {
                                if (courseList.get(i).getTcSn() != tcSn && courseList.get(i).getMark() != 1) {
                                    speciedList.add(courseList.get(i));
                                }
                            }
                            SharedPreferences sp = getContext().getSharedPreferences("LearnthematiclearningFragment", Context.MODE_PRIVATE);
                            int minPer = sp.getInt("minPer", 0);
                            int tphotoMark = sp.getInt("tphotoMark", 0);
                            int stId = sp.getInt("stId", 0);
                            int seqTMark = sp.getInt("seqTMark", 0);
                            int apromptMark = sp.getInt("apromptMark", 0);
                            //专题培训课程必须顺序学习；
                            if (positon == 0) {
                                showLoadingDialog("");
                                Bundle bundle = new Bundle();
                                bundle.putInt("tcsn", tcSn);
                                bundle.putInt("uschSn", uschSn);//专项培训用户学时序列号
                                bundle.putInt("sn", sn);//专项培训用户课程序列号
                                bundle.putInt("readLength", readLength);//保存时间
                                bundle.putSerializable("CourseDetailList", (Serializable) speciedList);
                                bundle.putInt("minPer", minPer);
                                bundle.putInt("tphotoMark", tphotoMark);
                                bundle.putInt("stId", stId);
                                bundle.putInt("apromptMark", apromptMark);
                                //startActivity(LearnthemticlearningDetailsActivity.class, bundle);
                                return;
                            }
                            speciedList1.clear();
                            for (int i = 0; i <= positon - 1; i++) {
                                if (courseList.get(i).getMark() == 0) {
                                    speciedList1.add(courseList.get(i));
                                }
                            }
                            if (seqTMark == 1 && speciedList1.size() > 0) {
                                TiShiDialog.Builder builder1 = new TiShiDialog.Builder(getContext());
                                builder1.setMessage("上一节课程需学习完成，才能继续当前课程");
                                builder1.setTitle("提示:");
                                builder1.setPositiveButton("我知道", new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();

                                    }
                                });
                                builder1.create().show();

                                return;
                            }
                            showLoadingDialog("");
                            Bundle bundle = new Bundle();
                            bundle.putInt("tcsn", tcSn);
                            bundle.putInt("uschSn", uschSn);//专项培训用户学时序列号
                            bundle.putInt("sn", sn);//专项培训用户课程序列号
                            bundle.putInt("readLength", readLength);//保存时间
                            bundle.putSerializable("CourseDetailList", (Serializable) speciedList);
                            bundle.putInt("minPer", minPer);
                            bundle.putInt("tphotoMark", tphotoMark);
                            bundle.putInt("stId", stId);
                            bundle.putInt("apromptMark", apromptMark);
                            //startActivity(LearnthemticlearningDetailsActivity.class, bundle);

                        } else if (mList.get(positon).getMark() == 1) {

                            TiShiDialog.Builder builder1 = new TiShiDialog.Builder(getContext());
                            builder1.setMessage("您已学习了该课程，继续浏览课程将不记录时间");
                            builder1.setTitle("提示:");
                            builder1.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                    SharedPreferences sp = getContext().getSharedPreferences("LearnthematiclearningFragment", Context.MODE_PRIVATE);
                                    int stId = sp.getInt("stId", 0);
                                    int tcSn = mList.get(positon).getTcSn();
                                    Bundle bundle = new Bundle();
                                    bundle.putInt("tcsn", tcSn);
                                    bundle.putInt("stId", stId);
                                    //startActivity(LearnthemticlearningDetailsAgainActivity.class, bundle);
                                    //设置你的操作事项
                                }
                            });
                            builder1.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();

                                }
                            });
                            builder1.create().show();

                        }
                    }
                }
            });
        }


    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return false;
    }


    private boolean forbidHourJudge() {
        boolean canStudy = false;

        int time1 = Integer.valueOf(forbidHour.split(",")[0]);
        int time2 = Integer.valueOf(forbidHour.split(",")[1]);
        //int nowHour=new Date().getHours();
        int nowHour = new Date().getHours();
        if (time1 < time2) { //2-5
            if (nowHour >= time1 && nowHour <= (time2 - 1)) {
                canStudy = false;
            } else {
                canStudy = true;
            }
        } else {
            if (nowHour >= time2 && nowHour <= (time1 - 1)) {
                canStudy = true;
            } else {
                canStudy = false;
            }
        }
        return canStudy;
    }


    //网络请求
    //2.23 设置重新学习专题（设置后需重新购买）
    private void userStcModMark() {
        LoginUserBean userInfo = UserManger.getUserInfo();
        String userId = userInfo.getData().getUserId();
        new Login().userStcModMark(uschSn + "", userId, "0", this, USERSTC_MOD_MARK);
    }
    private void getThematicLearnRecord(){//获取专题学习记录
        LoginUserBean userInfo = UserManger.getUserInfo();
        String userId=userInfo.getData().getUserId();
        new Course().findByCond(userId,12,this,THEMATIC_LEARNRECORD_DATA);
    }
}
